package com.gin.no_gay_alliance.fragment.user.tab;

import android.os.Bundle;

import com.gin.no_gay_alliance.adpter.user.IpLocationHistoryGroupAdapter;
import com.gin.no_gay_alliance.app.MyApplication;
import com.gin.no_gay_alliance.database.dao.IpLocationHistoryDao;
import com.gin.no_gay_alliance.database.entity.IpLocationHistory;
import com.gin.no_gay_alliance.databinding.FragmentUserCenterTabUserIpLocationBinding;
import com.gin.no_gay_alliance.exception.RequireParamException;
import com.gin.no_gay_alliance.fragment.base.BaseBindingFragment;
import com.gin.no_gay_alliance.viewmodel.IpLocationHistoryGroup;

import java.util.List;

/**
 * 用户中心 IP属地历史
 *
 * @author : ginstone
 * @version : v1.0.0
 * @since : 2023/12/26 15:30
 **/

public class UserIpLocationFragment extends BaseBindingFragment<FragmentUserCenterTabUserIpLocationBinding> {
    private static final String TAG = UserIpLocationFragment.class.getSimpleName();
    private static final String KEY_USER_ID = "USER_ID";
    private long userId;

    /**
     * onCreateView 的后续操作
     */
    @Override
    protected void onCreateView() {
        IpLocationHistoryDao ipLocationHistoryDao = MyApplication.app.ngaDatabase.ipLocationHistoryDao();
        List<IpLocationHistory> histories = ipLocationHistoryDao.listByUserId(userId);
        List<IpLocationHistoryGroup> data = IpLocationHistoryGroup.group(histories);
        b.rv.setAdapter(new IpLocationHistoryGroupAdapter(requireContext(), data));
    }

    /**
     * 获取和处理参数
     *
     * @param arguments 参数
     */
    @Override
    protected void handleArguments(Bundle arguments) {
        this.userId = arguments.getLong(KEY_USER_ID);
        if (this.userId == 0L) {
            throw new RequireParamException();
        }
    }

    public static UserIpLocationFragment newInstance(long userId) {
        UserIpLocationFragment fragment = new UserIpLocationFragment();
        Bundle bundle = new Bundle();
        bundle.putLong(KEY_USER_ID, userId);
        fragment.setArguments(bundle);
        return fragment;
    }
}